<?php

namespace app\model;

use think\Model;

class Points extends Model
{

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'points';

    public static function getdescription($item)
    {
        $data['addcustomer'] = "新增客户";
        $data['adddongtai'] = "写跟进";
        $data['upjieduan'] = "推进客户";
        $data['deal'] = "成交客户";

        return $data[$item];
    }

    public static function uppoints($params)
    {
        $uid = $params['uid'];
        $points = $params['points'];
        $old_points = Member::where('id', $uid)->value('points');
        //更新积分
        if ($points) {
            self::create([
                'weid' => weid(),
                'uid' => $params['uid'],
                'points' => $points,
                'old_points' => $old_points,
                'description' => $params['description'],
                'prefix' => $params['prefix'],
                'creat_time' => time(),
                'type' => $params['type'] ?? 1,
            ]);
            if ($params['prefix'] == 1) {
                Member::where('id', $uid)->inc('points', $points)->update();
            } else {
                Member::where('id', $uid)->dec('points', $points)->update();
            }
        }
    }
    public function member()
    {
        return $this->hasOne(Member::class, 'id', 'uid');
    }
}
